/**
 * 
 */
package org.o14x.alpha.ui.views.foldertree;

import java.io.File;

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;
import org.o14x.alpha.domain.ExplorerTab;
import org.o14x.alpha.ui.controller.UIController;

/**
 * The view showing the arborescence of folders.
 * 
 * @author Olivier DANGREAUX
 */
public class FolderTreeView extends ViewPart {
	/**
	 * ID
	 */
	public static final String ID = FolderTreeView.class.getName();

	/**
	 * The FolderTreeFilter of the folder tree.
	 */
	private FolderTreeFilter folderTreeFilter;

	/**
	 * The label provider of the folder tree.
	 */
	private FolderTreeLabelProvider folderTreeLabelProvider;

	/**
	 * The UIController
	 */
	private UIController uiController;

	/**
	 * The component that shows the folder tree.
	 */
	private TreeComposite treeComposite;

	@Override
	public void init(IViewSite site) throws PartInitException {
		if (uiController.getDomain().getCurrentExplorerTab() == null) {
			ExplorerTab explorerTab = uiController.getDomain().getExplorerTabManager().getNewExplorerTab();
			uiController.getDomain().setCurrentExplorerTab(explorerTab);
			explorerTab.navigate(File.listRoots()[0]);
		}

		super.init(site);
	}

	@Override
	public void createPartControl(Composite parent) {
		//		FilteredTree filteredTree = new FilteredTree(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL, new PatternFilter());
		//
		//		folderTreeViewer = filteredTree.getViewer();
		//		folderTreeViewer.setContentProvider(new FolderTreeContentProvider());
		//		folderTreeViewer.setLabelProvider(folderTreeLabelProvider);
		//		folderTreeViewer.addFilter(folderTreeFilter);
		//
		//		getSite().setSelectionProvider(folderTreeViewer);	
		//
		//		File[] roots = File.listRoots();
		//		folderTreeViewer.setInput(roots);

		treeComposite = new TreeComposite(parent, this);
		TreeViewer treeViewer = treeComposite.getNewTreeViewer();
		uiController.getDomain().getCurrentExplorerTab().setExplorerTreeViewer(treeViewer);
		//treeComposite.activateTreeViewer(treeViewer);
		//getViewSite().getPage().addSelectionListener(FolderTreeView.ID, new FolderTreeSelectionListener(uiController));

		update();
	}

	@Override
	public void setFocus() {
	}

	/**
	 * Update the folder tree according to the current folder.
	 */
	public void update() {
		ExplorerTab currentExplorerTab = uiController.getDomain().getCurrentExplorerTab();

		if (currentExplorerTab.getExplorerTreeViewer() == null) {
			currentExplorerTab.setExplorerTreeViewer(treeComposite.getNewTreeViewer());
		}		
		TreeViewer treeViewer = currentExplorerTab.getExplorerTreeViewer();
				
		treeComposite.activateTreeViewer(treeViewer);
		treeComposite.setSelection(currentExplorerTab.getFolder());
				
		FolderTreeInput folderTreeInput = (FolderTreeInput)treeViewer.getInput();
		folderTreeInput.refresh();
		treeViewer.refresh();

		//		IEditorPart activeEditor = uiController.getUi().getFolderEditorManager().getFolderEditor(currentExplorerTab.getId());
		//		if (activeEditor == null) {
		//			FolderEditorInput folderEditorInput = new FolderEditorInput(currentExplorerTab);
		//			try {
		//				PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(folderEditorInput, "org.o14x.alpha.ui.editors.FolderEditor");
		//			} catch (PartInitException e) {
		//				// TODO Auto-generated catch block
		//				e.printStackTrace();
		//			}
		//		}
	}
	


	/******************************************************************
	 * GETTERS AND SETTERS
	 ******************************************************************/

	/**
	 * Returns the value of folderTreeFilter.
	 *
	 * @return The value of folderTreeFilter.
	 */
	public FolderTreeFilter getFolderTreeFilter() {
		return folderTreeFilter;
	}

	/**
	 * Sets the value of folderTreeFilter.
	 *
	 * @param folderTreeFilter The value of folderTreeFilter to set.
	 */
	public void setFolderTreeFilter(FolderTreeFilter folderTreeFilter) {
		this.folderTreeFilter = folderTreeFilter;
	}

	/**
	 * Returns the value of folderTreeLabelProvider.
	 *
	 * @return The value of folderTreeLabelProvider.
	 */
	public FolderTreeLabelProvider getFolderTreeLabelProvider() {
		return folderTreeLabelProvider;
	}

	/**
	 * Sets the value of folderTreeLabelProvider.
	 *
	 * @param folderTreeLabelProvider The value of folderTreeLabelProvider to set.
	 */
	public void setFolderTreeLabelProvider(FolderTreeLabelProvider folderTreeLabelProvider) {
		this.folderTreeLabelProvider = folderTreeLabelProvider;
	}

	/**
	 * Returns the value of uiController.
	 *
	 * @return The value of uiController.
	 */
	public UIController getUiController() {
		return uiController;
	}

	/**
	 * Sets the value of uiController.
	 *
	 * @param uiController The value of uiController to set.
	 */
	public void setUiController(UIController uiController) {
		this.uiController = uiController;
	}

	/**
	 * Returns the value of treeComposite.
	 *
	 * @return The value of treeComposite.
	 */
	public TreeComposite getTreeComposite() {
		return treeComposite;
	}

	/**
	 * Sets the value of treeComposite.
	 *
	 * @param treeComposite The value of treeComposite to set.
	 */
	public void setTreeComposite(TreeComposite treeComposite) {
		this.treeComposite = treeComposite;
	}
}
